package com.labichaoka.chaoka.ui.record;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.labichaoka.chaoka.R;
import com.labichaoka.chaoka.base.BaseFragment;
import com.labichaoka.chaoka.entity.OrderListResponse;
import com.labichaoka.chaoka.ui.web.WebViewActivity;
import com.labichaoka.chaoka.utils.SharedPreferencesManager;
import com.labichaoka.chaoka.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class RepayFragment extends BaseFragment {

    @BindView(R.id.repay_list)
    RecyclerView repayList;
    @BindView(R.id.empty_layout)
    RelativeLayout emptyLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    @Override
    protected int initLayoutId() {
        return R.layout.fragment_repay;
    }

    @Override
    protected void initView() {
        repayList.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        Gson gson=new Gson();
        String orderListStr= SharedPreferencesManager.getInstance().getString("orderList","");
        if (TextUtils.isEmpty(orderListStr)){
            emptyLayout.setVisibility(View.VISIBLE);
            return;
        }

        OrderListResponse response=gson.fromJson(orderListStr,new TypeToken<OrderListResponse>() {}.getType());

        int count = response.getData().getOrderRepaymentList().size();
        if (count == 0) {
            emptyLayout.setVisibility(View.VISIBLE);
        }else{
            emptyLayout.setVisibility(View.GONE);
        }

        RepayOrderAdapter adapter=new RepayOrderAdapter(mContext,response.getData().getOrderRepaymentList());
        repayList.setAdapter(adapter);
        adapter.setOnItemClickListener((adapter1, view, position) -> {
            String orderId=response.getData().getOrderRepaymentList().get(position).getOrderNo();
            Bundle bundle=new Bundle();
            bundle.putString("orderId",orderId);
            bundle.putString("type","3");
            gotoActivity(mContext,WebViewActivity.class,bundle);
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
